
#include "includes.h"


/** Depracated
 */
yjk_obj_header *
heap_alloc(unsigned int size, int type, struct task_info *ti)
{
  return (yjk_obj_header *) (heap_alloc2(size + sizeof(yjk_obj_header), type, ti)).pointer;
}

/** Allocate an object.  Allocates an object on the heap, assuming caller accounts for object header size.
 * @param size size of object, in bytes, including header.
 * @param type type of object
 * @param ti pointer to task_info structure of task's heap to use, or NULL
 */
YJK_OBJECT
heap_alloc2(unsigned int size, int type, struct task_info *ti)
{
  unsigned int required_size = (size + 7) & 0xfffffffc;
  yjk_obj_header *a;
  
  //Allocate in current task if not specified
  if (ti == NULL) ti = task_ptr;
  
  if (((void *) ti->heap) + required_size < (void *) ti->heap_limit)
    {
      a = (yjk_obj_header *) ti->heap;
      a->tag = 0;
      a->free = 0;
      a->type = type;
      a->size = required_size;

      ti->heap = ((void *) ti->heap) + required_size;
      return (YJK_OBJECT) (void *) a;
    }

  else
    {
      printf("Out of heap space\n");
      printf("Couldn't allocate %d bytes.  Heap: 0x%x 0x%x\n", required_size, (int) ti->heap, (int) ti->heap_limit);
      exit(1);
    }

  return (YJK_OBJECT) (void *) 0x0;
}

